<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bootstrap Input, Button and Output</title>

</head>
<body>
    <div>
        <input type="text" id="inputBox" class="form-control" placeholder="输入内容">
    </div>
    <div>
        <button id="submitButton" type="button" class="btn btn-primary">发送请求</button>
    </div>
    <div>
        <textarea id="outputBox" rows="10" readonly></textarea>
    </div>

    <script>
        // JavaScript 代码开始
        // 获取元素
        var inputBox = document.getElementById('inputBox');
        var submitButton = document.getElementById('submitButton');
        var outputBox = document.getElementById('outputBox');

        let intervalId;
        const checkData = () => {
            // 获取输入框的内容
            var inputValue = inputBox.value;
            // 将输入内容转换为 JSON 数据
            var jsonData = JSON.stringify({ "data": inputValue });
            // 发送请求
            fetch('/fetch1_url_with_json_data', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: jsonData
            })
           .then(response => response.json())
           .then(data => {
                // 将响应数据显示在输出框中
                // data 是一个json对象
                console.log("data=",data);
                if (data.count%10 == 0)
                {
                    console.log("count == 10",data.count);
                    clearInterval(intervalId);
                }
                outputBox.value += JSON.stringify(data, null, 2) + '\n';
            })
           .catch(error => {
                console.error('Error:', error);
                outputBox.value = '请求失败，请检查网络或服务器状态。';
            });
        };

        // 为按钮添加点击事件监听器
        submitButton.addEventListener('click', function() {
            intervalId = setInterval(checkData, 1000);
        });
        // JavaScript 代码结束
    </script>
</body>
</html>